<template>
  <div>
    <div>
      <el-tag @click="handleAdd()">新增</el-tag>

    </div>

    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column label="编号" prop="eid"  width="50"> </el-table-column>

      <el-table-column label="账号" prop="ename"> </el-table-column>

      <el-table-column label="真实姓名" prop="realName"> </el-table-column>

      <el-table-column label="创建时间" width="220">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="手机号" prop="tel"></el-table-column>

      <el-table-column label="邮箱" prop="email"  width="160"></el-table-column>

      <el-table-column label="角色" prop="role.roleName"></el-table-column>

      <el-table-column label="用户状态" prop="status.statusText">
      </el-table-column>

      <el-table-column align="right" width="160">
        <template slot="header">
          <!-- slot-scope="scope" -->
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px; margin-left: -800px">
      <el-button @click="toggleSelection([tableData[1], tableData[2]])"
        >切换第二、第三行的选中状态</el-button
      >
      <el-button @click="toggleSelection()">取消选择</el-button>
    </div>

    <!-- 修改的模态框 -->
    <el-dialog title="员工信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="登录名" :label-width="formLabelWidth">
          <el-input v-model="form.ename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="真实姓名" :label-width="formLabelWidth">
          <el-input v-model="form.realName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.tel" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="用户状态"
          :label-width="addformLabelWidth"
          prop="stateCode"
        >
          <el-tooltip
            :content="'当前用户状态: ' + form.stateCode"
            placement="top"
          >
            <el-switch
              v-model="form.stateCode"
              active-color="#13ce66"
              inactive-color="#dcdfe6"
              active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </el-tooltip>
        </el-form-item>

        <el-form-item
          label="用户角色"
          :label-width="formLabelWidth"
          prop="role"
        >
          <el-select
            v-model="form.roleId"
            placeholder="请选择用户的角色"
          >
            <el-option
              v-for="item in roleList"
              :key="item.rid"
              :label="item.roleName"
              :value="item.rid"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateEmp()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 增加的模态框 -->
    <el-dialog title="员工信息" :visible.sync="adddialogFormVisible">
      <el-form :model="addform" :rules="rules">
        <el-form-item
          label="账号"
          :label-width="addformLabelWidth"
          prop="ename"
        >
          <el-input
            v-model="addform.ename"
            placeholder="请设置用户名"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="真实姓名"
          :label-width="addformLabelWidth"
          prop="realName"
        >
          <el-input v-model="addform.realName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="密码"
          :label-width="addformLabelWidth"
          prop="password"
        >
          <el-input
            type="password"
            v-model="addform.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="手机号"
          :label-width="addformLabelWidth"
          prop="tel"
        >
          <el-input
            v-model="addform.tel"
            autocomplete="off"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="邮箱"
          :label-width="addformLabelWidth"
          prop="email"
        >
          <el-input
            v-model="addform.email"
            autocomplete="off"
            placeholder="请输入邮箱"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="用户状态"
          :label-width="addformLabelWidth"
          prop="stateCode"
        >
          <el-tooltip
            :content="'当前用户状态: ' + addform.stateCode"
            placement="top"
          >
            <el-switch
              v-model="addform.stateCode"
              active-color="#13ce66"
              inactive-color="#dcdfe6"
              active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </el-tooltip>
        </el-form-item>

        <el-form-item
          label="用户角色"
          :label-width="formLabelWidth"
          prop="role"
        >
          <el-select
            v-model="addform.roleId"
            placeholder="请选择用户的角色"
          >
            <el-option
              v-for="item in roleList"
              :key="item.rid"
              :label="item.roleName"
              :value="item.rid"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="adddialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEmp()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [
        {
          eid: 1,
          email: "23456@qq.com",
          ename: "admin1",
          password: "123456",
          role: { rid: 2, roleName: "admin", roleExplain: "后台登录，管理" },
          roleId: 1,
          stateCode: 1,
          status: {
            statusCode: 1,
            statusText: "正常",
            statusDesc: "用户/管理员/景区正常",
          },
          tel: "13366665555",
        },
      ],
      search: "",
      multipleSelection: [],
      dialogFormVisible: false,
      formLabelWidth: "120px",
      form: {
        eid: 1,
        email: "23456@qq.com",
        ename: "admin1",
        password: "123456",
        roleId: 2,
        stateCode: 1,
        tel: "13366665555",
        index: "",
      },
      adddialogFormVisible: false,
      addformLabelWidth: "120px",
      addform: {
        ename: "xiaohu",
        realName: "王小虎",
        tel: "18899997777",
        email: "123456@qq.com",
        stateCode: "1",
      },
      roleList: [
        {rid: 3, roleName: "员工", roleExplain: "查看后台信息"}
      ],

      rules: {
        uname: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 4, max: 6, message: "长度在 4 到 6 个字符", trigger: "blur" },
        ],
        stateCode: [{ required: true, message: "请选择状态", tigger: "blur" }],
        realName: [
          { required: true, message: "请输入真实姓名", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 5 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  created: function () {
    //页面用户列表初始化
    this.$axios
      .get("emps")
      .then((res) => {
        if (res.data.statusCode == 200) {
          this.tableData = res.data.info;
        }
      })
      .catch((error) => {
        console.log(error);
      });

    //页面角色下拉框初始化
    this.$axios
      .get("roles")
      .then((res) => {
        if (res.data.statusCode == 200) {
          this.roleList = res.data.info;
        }
      })
      .catch((error) => {
        console.log(error);
      });
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
      this.form = row;
      this.form.stateCode = row.status.statusCode;
      this.form.index = index;
      this.dialogFormVisible = true;
    },

    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm(
        "此操作将永久删除" + row.ename + "的信息, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$axios
            .delete("delEmp?eid=" + row.eid)
            .then((res) => {
              if (res.data.statusCode == 200) {
                //更新表单???
                console.log(res.data.msg);
                this.tableData.splice(index, 1);
                this.$message({ message: "删除成功", type: "success" });
              } else {
                this.$message.error("删除失败");
              }
            })
            .catch((error) => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleAdd() {
      this.adddialogFormVisible = true;
    },

    updateEmp: function () {
      //使用axios向后端发出put请求
      this.$axios
        .put("updateEmp", this.form)
        .then((res) => {
          if (res.data.statusCode == 200) {
            //更新表单???
            console.log(res.data.msg);
            this.dialogFormVisible = false;
            this.$message({ message: "修改用户成功", type: "success" });
          } else {
            this.$message.error("修改用户失败");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },

    addEmp: function () {
      this.$axios
        .post("addEmp", this.addform)
        .then((res) => {
          if (res.data.statusCode == 200) {
            //更新表单???
            console.log(res.data.msg);
            this.tableData.push(res.data.info);
            this.adddialogFormVisible = false;
            this.$message({ message: "添加用户成功", type: "success" });
          } else {
            this.$message.error("添加用户失败");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
<style scoped>
</style>
